package com.ruoyi.business.common;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回结果类,将服务器端的数据封装成该类型返回给浏览器端
 * @param <T>
 * implements Serializable--实现序列化接口，因为Spring Cache缓存的类需要实现序列化接口
 */
@Data
public class Result<T> implements Serializable {

    private Integer code; //编码：1成功，0和其它数字为失败

    private String msg; //错误信息

    private T data; //数据

    private Map map = new HashMap(); //动态数据

    public static <T> Result<T> success(T object) {//请求成功，只需将数据封装成Result
        Result<T> r = new Result<T>();
        r.data = object;
        r.code = 1;
        return r;
    }

    public static <T> Result<T> error(String msg) {//请求失败，将错误信息封装进去
        Result r = new Result();
        r.msg = msg;
        r.code = 0;
        return r;
    }
    public static <T> Result<T> notLogin(String msg) {//请求失败，将错误信息封装进去
        Result r = new Result();
        r.msg = msg;
        r.code = 401;
        return r;
    }


    public static <T> Result<T> route(String msg) {//请求失败，将错误信息封装进去
        Result r = new Result();
        r.msg = msg;
        r.code = 403;
        return r;
    }

    public Result<T> add(String key, Object value) {//用于增加动态数据
        this.map.put(key, value);
        return this;
    }

}
